package com.mystyle.le.qjnewsproject.custom;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;

/**
 * Created by le on 2016/10/8.
 */
public class SlidingView extends HzCustomView {
    private boolean isMeasure = false;
    private LinearLayout sliding_container;
    private LinearLayout content_layout;
    private LinearLayout menu_layout;
    private int widthPixels;
    private int menuRight = 200;
    private int menuWidth = 0;

    public SlidingView(Context context) {
        super(context);
    }

    public SlidingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        widthPixels = context.getResources().getDisplayMetrics().widthPixels;//获取屏幕宽
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (!isMeasure){
            sliding_container = ((LinearLayout) getChildAt(0));
            content_layout = ((LinearLayout) sliding_container.getChildAt(0));
            menu_layout = ((LinearLayout) sliding_container.getChildAt(1));
            menuWidth = menu_layout.getLayoutParams().width = widthPixels - menuRight;
            content_layout.getLayoutParams().width = widthPixels;
            isMeasure = true;
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (changed){
            smoothScrollTo(0,0);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()){
            case MotionEvent.ACTION_UP:
               int scrollX = getScrollX();
                if (scrollX<menuWidth/2){
                    smoothScrollTo(0,0);
                }else {
                    smoothScrollTo(menuWidth,0);
                }

                break;
        }
        return super.onTouchEvent(ev);
    }
}
